<?php
namespace App\Dao\config;
use App\Dao\BaseDao;
use App\Models\config\ConfigModel;
use function Couchbase\defaultDecoder;
use Illuminate\Support\Facades\DB;
use App\Services\system\SystemLogServices;
use App\Exceptions\ApiException as HOException;
use Hlcreation\Basic\Helper;

class OrmDao extends  BaseDao
{
    public function setModel(){
        return ConfigModel::class;
    }


    //添加orm配置
    public function ormSaves($arr){

        DB::beginTransaction();
        try{

            $where = [
                'creator_uid'=>$arr['creator_uid'],
                'type'=>$arr['type'],
            ];

            ConfigModel::where($where)->forceDelete();
            $res['creator_uid'] = $arr['creator_uid']; $res['created_at'] = $arr['created_at'];
            unset($arr['creator_uid']);unset($arr['created_at']);
            $arr['type'] = 5;
            foreach ($arr as $k=>&$v){
                ConfigModel::create([
                    'menu_name'=>$k,
                    'value'=>$v,
                    'creator_uid'=>$res['creator_uid'],
                    'created_at'=>$res['created_at'],
                    'config_tab_id'=>40,
                    'desc'=>'orm配置',
                    'type'=>$arr['type'],
                    'status'=>$arr['status']
                ]);
            }

            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($res['creator_uid'],'创建系统配置','配置项名称:'.'orm配置'.'--配置项ID:'.'40');

            DB::commit();
            return true;
        }catch (\Exception $exception){
            DB::rollBack();
            throw new HOException($exception->getMessage());
        }

    }


    public function OrmInfos($uid,$type){
        $field = [
            'id',
            'menu_name',
            'value',
            'desc',
            'status',
            'creator_uid',
            'config_tab_id',
            'type',
            'created_at'
        ];
        return  ConfigModel::where('type',$type)->where('creator_uid',$uid)->select($field)->get()->toArray();
    }



    public function configInfo($uid){

        $data['sms'] = ConfigModel::where('creator_uid',$uid)->where('type',4)->select('menu_name','value','creator_uid','type','status')->get()->toArray();
        //短信
        $sms = [];
        foreach ($data['sms'] as $kk=>&$vv){
            $sms[$vv['menu_name']] = $vv['value'];
        }

        $data['orm'] = ConfigModel::where('creator_uid',$uid)->where('type',5)->select('menu_name','value','creator_uid','type','status')->get()->toArray();
        //dd($data['orm']);
        $orm = [];
        foreach ($data['orm'] as $kk=>&$vv){
            $orm[$vv['menu_name']] = $vv['value'];
        }
        //dd($orm);

        $data['upload_set']['aliyun_upload'] = ConfigModel::where('creator_uid',$uid)->where('type',1)->select('menu_name','value','creator_uid','type','status')->get()->toArray();

        $aliyun = [];
        foreach ($data['upload_set']['aliyun_upload'] as $kk=>&$vv){
            $aliyun[$vv['menu_name']] = $vv['value'];
        }


        $data['upload_set']['tengxunyun_upload'] = ConfigModel::where('creator_uid',$uid)->where('type',2)->select('menu_name','value','creator_uid','type','status')->get()->toArray();

        $tengxunyun = [];
        foreach ($data['upload_set']['tengxunyun_upload'] as $kk=>&$vv){
            $tengxunyun[$vv['menu_name']] = $vv['value'];
        }

        $data['upload_set']['qiniuyun_upload'] = ConfigModel::where('creator_uid',$uid)->where('type',3)->select('menu_name','value','creator_uid','type','status')->get()->toArray();

        $qiniuyun = [];
        foreach ($data['upload_set']['qiniuyun_upload'] as $kk=>&$vv){
            $qiniuyun[$vv['menu_name']] = $vv['value'];
        }



        $place['orm_set'] = $orm;
        $place['sign_sms'] = $sms;
        $place['upload_set']['aliyun_upload'] = $aliyun;
        $place['upload_set']['tengxun_upload'] = $tengxunyun;
        $place['upload_set']['qiniu_upload'] = $qiniuyun;
        $place['upload_set']['status'] =  $qiniuyun['status'];

        return $place;


    }





}
